package com.whj.serviceprice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whj.internalcommon.constant.CommonStatusEnum;
import com.whj.internalcommon.dto.PriceRules;
import com.whj.internalcommon.dto.ResponseResult;
import com.whj.serviceprice.mapper.PriceRulesMapper;
import com.whj.serviceprice.service.IPriceRulesService;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whj
 * @since 2023-01-29
 */
@Service
public class PriceRulesServiceImpl implements IPriceRulesService {

    @Autowired
    private PriceRulesMapper priceRulesMapper;

    /**
     * 修改计价规则
     *
     * @param priceRules
     * @return
     */
    @Override
    public ResponseResult add(PriceRules priceRules) {

        //拼接fareType
        String cityCode = priceRules.getCityCode();
        String vehicleType = priceRules.getVehicleType();
        priceRules.setFareType(cityCode + "$" + vehicleType);

//        //添加版本号
//        Map map = new HashMap<>();
//        map.put("city_code", cityCode);
//        map.put("vehicle_type", vehicleType);

        //问题1：增加了版本号，无法唯一确定， 问题二：找最大的版本号，需要排序
        //每修改一次版本号就加一
        QueryWrapper<PriceRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", cityCode);
        queryWrapper.eq("vehicle_type", vehicleType);
        queryWrapper.orderByDesc("fare_version");

        List<PriceRules> priceRulesList = priceRulesMapper.selectList(queryWrapper);
        Integer fareVersion = 0;
        if (priceRulesList.size() > 0) {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXIST.getCode(), CommonStatusEnum.PRICE_RULE_EXIST.getValue(), "");
        }
        //fareVersion = priceRulesList.get(0).getFareVersion();
        priceRules.setFareVersion(++fareVersion);

        priceRulesMapper.insert(priceRules);
        return ResponseResult.success("");
    }

    /**
     * 修改计价规则
     *
     * @param priceRules
     * @return
     */
    @Override
    public ResponseResult edit(PriceRules priceRules) {

        //拼接fareType
        String cityCode = priceRules.getCityCode();
        String vehicleType = priceRules.getVehicleType();
        priceRules.setFareType(cityCode + "$" + vehicleType);

//        //添加版本号
//        Map map = new HashMap<>();
//        map.put("city_code", cityCode);
//        map.put("vehicle_type", vehicleType);

        //问题1：增加了版本号，无法唯一确定， 问题二：找最大的版本号，需要排序
        //每修改一次版本号就加一
        QueryWrapper<PriceRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", cityCode);
        queryWrapper.eq("vehicle_type", vehicleType);
        queryWrapper.orderByDesc("fare_version");

        List<PriceRules> priceRulesList = priceRulesMapper.selectList(queryWrapper);
        Integer fareVersion = 0;
        if (priceRulesList.size() > 0) {

            PriceRules lastPriceRules = priceRulesList.get(0);
            Double unitPricePerMinute = lastPriceRules.getUnitPricePerMinute();
            Double unitPricePerMile = lastPriceRules.getUnitPricePerMile();
            Integer startMile = lastPriceRules.getStartMile();
            Double startFare = lastPriceRules.getStartFare();

            if (unitPricePerMile.doubleValue() == priceRules.getUnitPricePerMile().doubleValue()
                    && unitPricePerMinute.doubleValue() == priceRules.getUnitPricePerMinute().doubleValue()
                    && startMile.intValue() == priceRules.getStartMile().intValue()
                    && startFare.doubleValue() == priceRules.getStartFare().doubleValue()) {
                return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_NOT_EDIT.getCode(), CommonStatusEnum.PRICE_RULE_NOT_EDIT.getValue(), "");
            }

            fareVersion = priceRulesList.get(0).getFareVersion();
        }
        priceRules.setFareVersion(++fareVersion);

        priceRulesMapper.insert(priceRules);
        return ResponseResult.success("");
    }

    /**
     * 查询最新的计价规则版本
     *
     * @param fareType
     * @return
     */
    @Override
    public ResponseResult<PriceRules> getNewestVersion(String fareType) {

        QueryWrapper<PriceRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fare_type", fareType);
        queryWrapper.orderByDesc("fare_version");

        List<PriceRules> priceRulesList = priceRulesMapper.selectList(queryWrapper);

        if (priceRulesList.size() > 0) {
            return ResponseResult.success(priceRulesList.get(0));
        } else {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue(), "");
        }

    }

    /**
     * 判断当前计价规则是否是最新的
     *
     * @param fareType
     * @param fareVersion
     * @return
     */
    @Override
    public ResponseResult<Boolean> getVersionIsNew(String fareType, Integer fareVersion) {
        ResponseResult<PriceRules> newestVersionPriceRules = getNewestVersion(fareType);
        if (newestVersionPriceRules.getCode() == CommonStatusEnum.PRICE_RULE_EMPTY.getCode()) {
            //return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue(), "");
            return ResponseResult.success(false);
        }
        PriceRules priceRules = newestVersionPriceRules.getData();
        Integer fareVersionDB = priceRules.getFareVersion();
        if (fareVersionDB > fareVersion) {
            return ResponseResult.success(false);
        } else {
            return ResponseResult.success(true);
        }
    }

    /**
     * 根据城市编码和车型查询计价规则是否存在
     *
     * @param priceRules
     * @return
     */
    @Override
    public ResponseResult<Boolean> ifExists(PriceRules priceRules) {

        String cityCode = priceRules.getCityCode();
        String vehicleType = priceRules.getVehicleType();
        QueryWrapper<PriceRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", cityCode);
        queryWrapper.eq("vehicle_type", vehicleType);
        queryWrapper.orderByDesc("fare_version");
        List<PriceRules> priceRulesList = priceRulesMapper.selectList(queryWrapper);

        if (priceRulesList.size() > 0) {
            return ResponseResult.success(true);
        } else {
            return ResponseResult.success(false);
        }
    }
}
